#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include <time.h>

typedef struct message_people{
    uint8_t id;
    uint16_t now_time;
    char name[20];
    char major[20];
}msg_people_t;


osMessageQueueId_t msg_queue_id;

// 发送消息
void msg_demo_thread1(void *arg){
    char names[10][20] = {"zhangsan", "lisi", "wangwu", "zhaoliu", "qianqi", "sunba", "jiu", "tian", "ren", "ji"};
    char majors[10][20] = {"computer", "math", "physics", "chemistry", "biology", "english", "chinese", "history", "geography", "politics"};
    for(int i=0; i<10; i++){
        msg_people_t msg_people;
        msg_people.id = i;
        msg_people.now_time = time(NULL);
        strcpy(msg_people.name, names[i]);
        strcpy(msg_people.major, majors[i]);
        osMessageQueuePut(msg_queue_id, &msg_people, 0, osWaitForever);
        printf("[put] msg_demo_thread1: %d, %s, %s\n", msg_people.id, msg_people.name, msg_people.major);
        osDelay(1000);
    }
}

// 接收消息
void msg_demo_thread2(void *arg){
    msg_people_t msg_people;
    uint8_t msg_prio;
    uint32_t timeout = osWaitForever;
    while(1){
        osDelay(1200);
        osMessageQueueGet(msg_queue_id, &msg_people, &msg_prio, timeout);
        printf("[get] msg_demo_thread2: %d, %s, %s\n", msg_people.id, msg_people.name, msg_people.major);
    }
}

void msg_demo(void){
    msg_queue_id = osMessageQueueNew(16, sizeof(msg_people_t), NULL);
    if(msg_queue_id == NULL){
        printf("Create message queue failed\n");
    }
    else{
        printf("[msg_queue] message queue id is %d\n", msg_queue_id);
    }

    osThreadId_t thread_id1;
    osThreadAttr_t thread_attr1;

    thread_attr1.name = "msg_demo_thread1";
    thread_attr1.attr_bits = 0U;
    thread_attr1.cb_mem = NULL;
    thread_attr1.cb_size = 0U;
    thread_attr1.stack_mem = NULL;
    thread_attr1.stack_size = 1024*16;
    thread_attr1.priority = osPriorityNormal;

    thread_id1 = osThreadNew((osThreadFunc_t)msg_demo_thread1, NULL, &thread_attr1);
    if(thread_id1 == NULL){
        printf("[msg_demo_thread1] create failed\n");
    }
    else{
        printf("[msg_demo_thread1] Thread1 id is %d\n", thread_id1);
    }

    osThreadId_t thread_id2;
    osThreadAttr_t thread_attr2;

    thread_attr2.name = "msg_demo_thread2";
    thread_attr2.attr_bits = 0U;
    thread_attr2.cb_mem = NULL;
    thread_attr2.cb_size = 0U;
    thread_attr2.stack_mem = NULL;
    thread_attr2.stack_size = 1024*4;
    thread_attr2.priority = osPriorityNormal1;
    
    thread_id2 = osThreadNew((osThreadFunc_t)msg_demo_thread2, NULL, &thread_attr2);
    if(thread_id2 == NULL){
        printf("[msg_demo_thread2] create failed\n");
    }
    else{
        printf("[msg_demo_thread2] Thread2 id is %d\n", thread_id2);
    }

    // 打印消息队列信息
    while(1){
        osDelay(2500);
        printf("消息队列信息：\n");
        printf("队列大小：%d\n", osMessageQueueGetCapacity(msg_queue_id));
        printf("消息大小：%d\n", osMessageQueueGetMsgSize(msg_queue_id));
        printf("当前消息数：%d\n", osMessageQueueGetCount(msg_queue_id));
        printf("当前空间数：%d\n", osMessageQueueGetSpace(msg_queue_id));
    }
}

SYS_RUN(msg_demo);